package gold.digger;

import gold.vo.ListNode;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC86 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public ListNode partition(ListNode head, int x) {
            if (null == head) return head;

            ListNode cur = head, smallHead = null, smallTail = null, bigHead = null, bigTail = null;
            while (cur != null) {
                if (cur.val < x) {
                    if (null == smallTail) {
                        smallTail = cur;
                        smallHead = cur;
                    } else {
                        smallTail.next = cur;
                        smallTail = smallTail.next;
                    }
                } else {
                    if (null == bigTail) {
                        bigTail = cur;
                        bigHead = cur;
                    } else {
                        bigTail.next = cur;
                        bigTail = bigTail.next;
                    }
                }

                cur = cur.next;
            }

            if (null != bigTail) bigTail.next = null;
            if (null != smallTail) smallTail.next = bigHead;
            return null == smallHead ? bigHead : smallHead;
        }
    }

    public void run() {
        Solution solution = new Solution();
        ListNode head = ListNode.createList("1");
        System.out.println(solution.partition(head, 2));
    }

    public static void main(String[] args) throws Exception {
        LC86 an = new LC86();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
